package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumeMapper;
import com.goods.business.service.ConsumeService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author XiaoHong
 * @description
 * @date 2022/2/18
 */
@Service
public class ConsumeServiceImpl implements ConsumeService {
    @Autowired
    private ConsumeMapper consumeMapper;

    //获取物资去处列表
    @Override
    public PageVO<ConsumerVO> findConsumerList(ConsumerVO consumerVO, Integer pageNum, Integer pageSize) {
        /*
        * pageNum: 1
            pageSize: 10
            name: 哈哈哈
            address: 周口市
            contact: 史开心
        *
        * */
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //设置查询对象  其实有点相似  new QueryWrapper
        Example example = new Example(Consumer.class);
        //创建查询条件
        Example.Criteria criteria = example.createCriteria();
        //判断前端传到后台的数据是否存在address，模糊查询
        if (!"".equals(consumerVO.getAddress()) && null != consumerVO.getAddress()) {
            //存在
            criteria.andLike("address", "%" + consumerVO.getAddress() + "%");
        }
        //判断前端传到后台的数据是否存在联系人contact，精准查询
        if (!"".equals(consumerVO.getContact()) && null != consumerVO.getContact()) {
            //存在
            criteria.andEqualTo("contact", consumerVO.getContact());
        }
        //判断前端传到后台的数据是否存在具体地点name， 精准查询
        if (!"".equals(consumerVO.getName()) && null != consumerVO.getName()) {
            //存在
            criteria.andEqualTo("name", consumerVO.getName());
        }
        //进行查询
        List<Consumer> ConsumerList = consumeMapper.selectByExample(example);
        //封装返回数据
        //进行偷天换日
        List<ConsumerVO> consumerVOList = ConsumerList.stream().map(consumer -> {
            ConsumerVO vo = new ConsumerVO();
            BeanUtils.copyProperties(consumer, vo);
            return vo;
        }).collect(Collectors.toList());
        //将数据对象放入到page中，可以计算出总共的数量.(注意当开启分页之后，查询出来的数据其实已经是当前的页的数据了)
        PageInfo<Supplier> supplierPageInfo = new PageInfo(ConsumerList);
        System.out.println(consumerVOList);
        System.out.println(supplierPageInfo.getTotal());
        return new PageVO<ConsumerVO>(supplierPageInfo.getTotal(),   //这个地方的总数量，如果是从pageInfo中拿出来的就是总数居，而不是当前页中数据的数量
                consumerVOList);
    }

    //添加物资去处
    @Override
    public void add(Consumer consumer) {
        //设置时间
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        //进行添加数据
        consumeMapper.insert(consumer);
    }

    @Override
    public void update(Consumer consumer, Long consumerId) {
        consumer.setId(consumerId);
        consumer.setModifiedTime(new Date());
        consumeMapper.updateByPrimaryKey(consumer);
    }

    @Override
    public void delete(Long consumerId) {
        consumeMapper.deleteByPrimaryKey(consumerId);
    }

    @Override
    public Consumer edit(Long consumerId) {
        return consumeMapper.selectByPrimaryKey(consumerId);
    }

    //    http://www.localhost:8989/business/consumer/findAll
    //获取所有的去向
    @Override
    public List<Consumer> findAll() {
        return consumeMapper.selectAll();
    }
}
